<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2024/2/27
 * Time: 9:51
 */

namespace app\api\controller;

use app\api\model\ShopCar AS ShopCarModel;

class Shopcar extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 新增购物车
     * goods_id          商品ID
     * packaging_id      包装ID
     * spec_id           规格ID
     * spec_number       数量
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addShopCar(){
        $goods_id = input('goods_id');
        $packaging_id = input('packaging_id');
        $spec_id = input('spec_id');
        $spec_number = input('spec_number');

        if(!$goods_id) return json(['status' => 0, 'msg' => $this->lanMsg->goodsIDErr[$this->lanType]]);
        if(!$packaging_id) return json(['status' => 0, 'msg' => $this->lanMsg->packagingIDErr[$this->lanType]]);
        if(!$spec_id) return json(['status' => 0, 'msg' => $this->lanMsg->specIDErr[$this->lanType]]);
        if(!$spec_number) return json(['status' => 0, 'msg' => $this->lanMsg->numberMustHave[$this->lanType]]);

        $findWhere = [
            'sc_user_id' => $this->user_id,
            'sc_goods_id' => $goods_id,
            'sc_packaging_id' => $packaging_id,
            'sc_spec_id' => $spec_id
        ];
        $hasIn = ShopCarModel::findData($findWhere, 'sc_id, sc_number');
        if($hasIn){ // 更新
            $updateWhere = ['sc_id' => $hasIn['sc_id']];
            $update = [
                'sc_number' => $hasIn['sc_number'] + $spec_number
            ];
            $res = ShopCarModel::updateData($updateWhere, $update);
        }else{
            $insert = [
                'sc_user_id' => $this->user_id,
                'sc_goods_id' => $goods_id,
                'sc_packaging_id' => $packaging_id,
                'sc_spec_id' => $spec_id,
                'sc_number' => $spec_number,
                'sc_time' => initTime()
            ];
            $res = ShopCarModel::insertData($insert);
        }
        if($res){
            return json(['status' => 200, 'msg' => $this->lanMsg->doSuccess[$this->lanType]]);
        }else{
            return json(['status' => 0, 'msg' => $this->lanMsg->systemErr[$this->lanType]]);
        }
    }

    /**
     * 购物车列表
     * page     页码
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function shopCarList(){
        $page = input('page') ? input('page') : 1;

        $where = ['sc_user_id' => $this->user_id];
        $field = 'sc_id, goods_id, goods_thumb, packaging_name, spec_value, spec_cu_price, spec_price, 
        sc_number, goods_name_en, goods_name_es, goods_name_tp';

        $shopCar = ShopCarModel::selectJoinData($where, $field, $page)->toArray();
        if($shopCar){
            foreach ($shopCar as $k => $v){
                $shopCar[$k]['totalPrice'] = $v['sc_number'] * $v['spec_cu_price']; // 总价
                if($v['goods_thumb']){
                    $shopCar[$k]['goods_thumb'] = http_type() . $v['goods_thumb']; // 缩略图
                }

            }
            return json(['status' => 200, 'msg' => $shopCar]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }

    /**
     * 设置购物车数量
     * sc_id       购物车ID
     * number      设置的购物车数量
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function setNumber(){
        $sc_id = input('sc_id');
        $number = input('number') ? input('number') : 1;

        if(!$sc_id) return json(['status' => 0, 'msg' => $this->lanMsg->shopCarIDErr[$this->lanType]]);

        $res = ShopCarModel::updateData(['sc_id' => $sc_id], ['sc_number' => $number]);
        if($res){
            return json(['status' => 200, 'msg' => $this->lanMsg->doSuccess[$this->lanType]]);
        }else{
            return json(['status' => 0, 'msg' => $this->lanMsg->systemErr[$this->lanType]]);
        }
    }

    /**
     * 删除购物车商品
     * sc_ids     购物车ID，逗号隔开
     * @return \think\response\Json
     */
    public function delShopCar(){
        $sc_ids = input('sc_ids');

        if(!$sc_ids) return json(['status' => 0, 'msg' => $this->lanMsg->chooseDelShopCar[$this->lanType]]);

        $res = ShopCarModel::delData(['sc_id' => ['IN', $sc_ids]]);
        if($res){
            return json(['status' => 200, 'msg' => $this->lanMsg->doSuccess[$this->lanType]]);
        }else{
            return json(['status' => 0, 'msg' => $this->lanMsg->systemErr[$this->lanType]]);
        }
    }

    /**
     * 统计购物车数量
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function shopCarNumber(){
        $number = ShopCarModel::sumData(['sc_user_id' => $this->user_id]);
        return json(['status' => 200, 'msg' => $number]);
    }
}